#include "WidgetCompare3Driver.hpp"


WidgetCompare3Driver::WidgetCompare3Driver(const char *pRootPath, const char *pIniUIFilename, const char *pIniDriverMappingFilename, const char *pIniVehiculeClassMappingFilename)
{
	initialized = false;
	rootPath = pRootPath; 
	iniDriverMappingFilename = pIniDriverMappingFilename;
	iniVehiculeClassMappingFilename = pIniVehiculeClassMappingFilename;

	nbProfiles = GetPrivateProfileInt("Profiles", "count", 0, pIniUIFilename);
	for (int i = 0 ; i < nbProfiles; i++) {
		sprintf(tmp, "Profile.%d", (i+1));
		profiles[i].widgetOffsetX = (signed int)GetPrivateProfileInt(tmp, "Compare3Driver.offsetX", 0, pIniUIFilename);
		profiles[i].widgetOffsetY = (signed int)GetPrivateProfileInt(tmp, "Compare3Driver.offsetY", 0, pIniUIFilename);
		profiles[i].hide = GetPrivateProfileInt(tmp, "Compare3Driver.hide", 0, pIniUIFilename) != 0;
	}


	//std::string str = rootPath;
	GetPrivateProfileString("UI Widgets", "Compare3Driver.ini", "", tmp, 999, pIniUIFilename);
	//str.append(tmp);
	sprintf(iniUIFilename, "%s%s", rootPath, tmp);
	//iniUIFilename = str.c_str(); //pIniUIFilename; 
	available = GetPrivateProfileInt("Compare3Driver", "available", 0, iniUIFilename) != 0;

	textColorForViewedDriver = 0;
	textFontForViewedDriver = 0;
	for (int i = 0 ; i < 3; i++) {
		bestStr[i][0] = 0;
		lastStr[i][0] = 0;
	}
}

void WidgetCompare3Driver::init(const ScreenInfoV01 &info, float ratio, float ratioWidth, float ratioHeight, Sprite spriteList[]) {
	if (!available) {
		initialized = true;
		return;
	}

	for (int i = 0 ; i < nbProfiles; i++) {
		profiles[i].widgetOffsetX *= ratioWidth;
		profiles[i].widgetOffsetY *= ratioHeight;
	}


	int posX = (int)(ratioWidth * GetPrivateProfileInt("Compare3Driver", "image.left", 40, iniUIFilename));//40;
	int posY = (int)(ratioHeight * GetPrivateProfileInt("Compare3Driver", "image.top", 550, iniUIFilename));//550;

	//LPDIRECT3DDEVICE9 dev = static_cast<LPDIRECT3DDEVICE9>(info.mDevice);	
	

	bgImageData.spriteIndex = GetPrivateProfileInt("Compare3Driver", "image.id", 0, iniUIFilename) -1;
	if (bgImageData.spriteIndex > -1) {
		bgImageData.left = posX;
		bgImageData.top = posY;
		bgImageData.width = (int)(GetPrivateProfileInt("Compare3Driver", "image.width", (int)spriteList[bgImageData.spriteIndex].width, iniUIFilename) * ratioWidth);
		bgImageData.height = (int)(GetPrivateProfileInt("Compare3Driver", "image.height", (int)spriteList[bgImageData.spriteIndex].height, iniUIFilename) * ratioHeight);
		bgImageData.colorIndex = GetPrivateProfileInt("Compare3Driver", "image.color.id", 1, iniUIFilename) -1;
	}

	textColorForViewedDriver = GetPrivateProfileInt("Compare3Driver", "viewed.driver.text.font.color.id", 0, iniUIFilename) - 1;
	textFontForViewedDriver = GetPrivateProfileInt("Compare3Driver", "viewed.driver.text.font.id", 0, iniUIFilename) - 1;

	for (int i = 0; i < 3; i++) {
		// Driver Number
		sprintf(tmp, "Compare3Driver.Driver%d.Number", (i+1));
		numberAvailable[i] = GetPrivateProfileInt(tmp, "available", 0, iniUIFilename) != 0;
		if (numberAvailable[i]) {
			numberTextData[i].fontIndex = GetPrivateProfileInt(tmp, "text.font.id", 1, iniUIFilename) - 1;
			numberTextData[i].alignment = GetPrivateProfileInt(tmp, "text.alignment", 1, iniUIFilename);
			numberTextData[i].colorIndex = GetPrivateProfileInt(tmp, "text.font.color.id", 1, iniUIFilename) - 1;
			numberTextData[i].rect.left = (int)(posX + GetPrivateProfileInt(tmp, "text.left", 0, iniUIFilename) * ratioWidth);
			numberTextData[i].rect.top = (int)(posY + GetPrivateProfileInt(tmp, "text.top", 0, iniUIFilename) * ratioHeight);
			numberTextData[i].rect.right = (int)(posX + GetPrivateProfileInt(tmp, "text.right", 100, iniUIFilename) * ratioWidth);
			numberTextData[i].rect.bottom = (int)(posY + GetPrivateProfileInt(tmp, "text.bottom", 100, iniUIFilename) * ratioHeight);
		}


		// driver place
		sprintf(tmp, "Compare3Driver.Driver%d.Place", (i+1));
		placeAvailable[i] = GetPrivateProfileInt(tmp, "available", 0, iniUIFilename) != 0;
		if (placeAvailable[i]) {
			//rctDriverPlace.left = spBackground->position.x + (202 + 2) * ratioWidth;
			//rctDriverPlace.top = spBackground->position.y + (18 + 4) * ratioHeight;
			//rctDriverPlace.right = spBackground->position.x + 236 * ratioWidth;
			//rctDriverPlace.bottom = spBackground->position.y + 57 * ratioHeight;
			placeTextData[i].fontIndex = GetPrivateProfileInt(tmp, "text.font.id", 1, iniUIFilename) - 1;
			placeTextData[i].alignment = GetPrivateProfileInt(tmp, "text.alignment", 1, iniUIFilename);
			placeTextData[i].colorIndex = GetPrivateProfileInt(tmp, "text.font.color.id", 1, iniUIFilename) - 1;
			placeTextData[i].rect.left = (int)(posX + GetPrivateProfileInt(tmp, "text.left", 0, iniUIFilename) * ratioWidth);
			placeTextData[i].rect.top = (int)(posY + GetPrivateProfileInt(tmp, "text.top", 0, iniUIFilename) * ratioHeight);
			placeTextData[i].rect.right = (int)(posX + GetPrivateProfileInt(tmp, "text.right", 100, iniUIFilename) * ratioWidth);
			placeTextData[i].rect.bottom = (int)(posY + GetPrivateProfileInt(tmp, "text.bottom", 100, iniUIFilename) * ratioHeight);

		} 

		// category
		sprintf(tmp, "Compare3Driver.Driver%d.Category", (i+1));
		classAvailable[i] =  GetPrivateProfileInt(tmp, "available", 0, iniUIFilename) != 0;
		if (classAvailable[i]) {
			classTextData[i].rect.left = (int)(posX + GetPrivateProfileInt(tmp, "text.left", 0, iniUIFilename) * ratioWidth);
			classTextData[i].rect.top = (int)(posY + GetPrivateProfileInt(tmp, "text.top", 0, iniUIFilename) * ratioHeight);
			classTextData[i].rect.right = (int)(posX + GetPrivateProfileInt(tmp, "text.right", 100, iniUIFilename) * ratioWidth);
			classTextData[i].rect.bottom = (int)(posY + GetPrivateProfileInt(tmp, "text.bottom", 100, iniUIFilename) * ratioHeight);

			classTextData[i].fontIndex = GetPrivateProfileInt(tmp, "text.font.id", 1, iniUIFilename) - 1;
			classTextData[i].colorIndex = GetPrivateProfileInt(tmp, "text.font.color.id", 1, iniUIFilename) - 1;
			classTextData[i].alignment = GetPrivateProfileInt(tmp, "text.alignment", 1, iniUIFilename);

			classImageData[i].spriteIndex = GetPrivateProfileInt(tmp, "image.id", 0, iniUIFilename) -1;
			if (classImageData[i].spriteIndex > -1) {
				classImageData[i].left = (int)(posX + GetPrivateProfileInt(tmp, "image.left", 0, iniUIFilename) * ratioWidth);
				classImageData[i].top = (int)(posY + GetPrivateProfileInt(tmp, "image.top", 0, iniUIFilename) * ratioHeight);
				classImageData[i].width = (int)(GetPrivateProfileInt(tmp, "image.width", (int)spriteList[classImageData[i].spriteIndex].width, iniUIFilename) * ratioWidth);
				classImageData[i].height = (int)(GetPrivateProfileInt(tmp, "image.height", (int)spriteList[classImageData[i].spriteIndex].height, iniUIFilename) * ratioHeight);
				classImageData[i].colorIndex = GetPrivateProfileInt(tmp, "image.color.id", 1, iniUIFilename) -1;
			}
			classOriginalImageColorIndex[i] = classImageData[i].colorIndex;
			classOriginalTextColorIndex[i] = classTextData[i].colorIndex;
		}


		// class name
		sprintf(tmp, "Compare3Driver.Driver%d.ClassName", (i+1));
		classNameAvailable[i] =  GetPrivateProfileInt(tmp, "available", 0, iniUIFilename) != 0;
		if (classNameAvailable[i]) {
			classNameTextData[i].rect.left = (int)(posX + GetPrivateProfileInt(tmp, "text.left", 0, iniUIFilename) * ratioWidth);
			classNameTextData[i].rect.top = (int)(posY + GetPrivateProfileInt(tmp, "text.top", 0, iniUIFilename) * ratioHeight);
			classNameTextData[i].rect.right = (int)(posX + GetPrivateProfileInt(tmp, "text.right", 100, iniUIFilename) * ratioWidth);
			classNameTextData[i].rect.bottom = (int)(posY + GetPrivateProfileInt(tmp, "text.bottom", 100, iniUIFilename) * ratioHeight);

			classNameTextData[i].fontIndex = GetPrivateProfileInt(tmp, "text.font.id", 1, iniUIFilename) - 1;
			classNameTextData[i].colorIndex = GetPrivateProfileInt(tmp, "text.font.color.id", 1, iniUIFilename) - 1;
			classNameTextData[i].alignment = GetPrivateProfileInt(tmp, "text.alignment", 1, iniUIFilename);
		}

		// driver name
		sprintf(tmp, "Compare3Driver.Driver%d.Name", (i+1));
		driverNameAvailable[i] =  GetPrivateProfileInt(tmp, "available", 0, iniUIFilename) != 0;
		if (driverNameAvailable[i]) {
			driverNameTextData[i].rect.left = (int)(posX + GetPrivateProfileInt(tmp, "text.left", 0, iniUIFilename) * ratioWidth);
			driverNameTextData[i].rect.top = (int)(posY + GetPrivateProfileInt(tmp, "text.top", 0, iniUIFilename) * ratioHeight);
			driverNameTextData[i].rect.right = (int)(posX + GetPrivateProfileInt(tmp, "text.right", 100, iniUIFilename) * ratioWidth);
			driverNameTextData[i].rect.bottom = (int)(posY + GetPrivateProfileInt(tmp, "text.bottom", 100, iniUIFilename) * ratioHeight);

			driverNameTextData[i].fontIndex = GetPrivateProfileInt(tmp, "text.font.id", 1, iniUIFilename) - 1;
			driverNameTextData[i].colorIndex = GetPrivateProfileInt(tmp, "text.font.color.id", 1, iniUIFilename) - 1;
			driverNameTextData[i].alignment = GetPrivateProfileInt(tmp, "text.alignment", 1, iniUIFilename);
		}

		// best time
		sprintf(tmp, "Compare3Driver.Driver%d.BestLaptime", (i+1));
		bestAvailable[i] =  GetPrivateProfileInt(tmp, "available", 0, iniUIFilename) != 0;
		if (bestAvailable[i]) {
			bestTextData[i].rect.left = (int)(posX + GetPrivateProfileInt(tmp, "text.left", 0, iniUIFilename) * ratioWidth);
			bestTextData[i].rect.top = (int)(posY + GetPrivateProfileInt(tmp, "text.top", 0, iniUIFilename) * ratioHeight);
			bestTextData[i].rect.right = (int)(posX + GetPrivateProfileInt(tmp, "text.right", 100, iniUIFilename) * ratioWidth);
			bestTextData[i].rect.bottom = (int)(posY + GetPrivateProfileInt(tmp, "text.bottom", 100, iniUIFilename) * ratioHeight);

			bestTextData[i].fontIndex = GetPrivateProfileInt(tmp, "text.font.id", 1, iniUIFilename) - 1;
			bestTextData[i].colorIndex = GetPrivateProfileInt(tmp, "text.font.color.id", 1, iniUIFilename) - 1;
			bestTextData[i].alignment = GetPrivateProfileInt(tmp, "text.alignment", 1, iniUIFilename);
		}
		// last time
		sprintf(tmp, "Compare3Driver.Driver%d.LastLaptime", (i+1));
		lastAvailable[i] =  GetPrivateProfileInt(tmp, "available", 0, iniUIFilename) != 0;
		if (lastAvailable[i]) {
			lastTextData[i].rect.left = (int)(posX + GetPrivateProfileInt(tmp, "text.left", 0, iniUIFilename) * ratioWidth);
			lastTextData[i].rect.top = (int)(posY + GetPrivateProfileInt(tmp, "text.top", 0, iniUIFilename) * ratioHeight);
			lastTextData[i].rect.right = (int)(posX + GetPrivateProfileInt(tmp, "text.right", 100, iniUIFilename) * ratioWidth);
			lastTextData[i].rect.bottom = (int)(posY + GetPrivateProfileInt(tmp, "text.bottom", 100, iniUIFilename) * ratioHeight);

			lastTextData[i].fontIndex = GetPrivateProfileInt(tmp, "text.font.id", 1, iniUIFilename) - 1;
			lastTextData[i].colorIndex = GetPrivateProfileInt(tmp, "text.font.color.id", 1, iniUIFilename) - 1;
			lastTextData[i].alignment = GetPrivateProfileInt(tmp, "text.alignment", 1, iniUIFilename);
		}
	}
	initialized = true;
}

void WidgetCompare3Driver::updateData(const ScoringInfoV01 &info, DriverInfo orderedDriverInfos[], int viewedPlace, TimingInfoPerCategoryMap & timingInfoPerCategoryMap) {
	if (!available) return;
	if (viewedPlace <= 0) return;

	int startDriverIndex;
	DriverInfo & viewedDi = orderedDriverInfos[viewedPlace - 1];
	
	nbDrivers = info.mNumVehicles;
	if (viewedDi.place == 1) {
		startDriverIndex = 0;
		viewedPilotColorIndex = 0;
	} else if (viewedDi.place == nbDrivers) {
		startDriverIndex = (viewedDi.place - 1) - 2;
		viewedPilotColorIndex = 2;
	} else {
		startDriverIndex = (viewedDi.place - 1) - 1;
		viewedPilotColorIndex = 1;
	}
	
	//std::string strKey;
	//TimingInfo * catTiming;
	for (int i = 0; i < 3; i++) {
		if (startDriverIndex + i + 1 > nbDrivers) break;

		DriverInfo & di = orderedDriverInfos[startDriverIndex + i];
		
		//strKey = "C_";
		//strKey.append(di.mCategoryName);
		//TimingInfoPerCategoryMap::iterator it = timingInfoPerCategoryMap.find(strKey);
		//catTiming = &it->second;
		
		memcpy(&mPlace[i], &di.place, 1);
		//if (catTiming->bestPlaceID == di.mID) {
		//	firstPlaceInCat[i] = true;
		//} else {
		//	firstPlaceInCat[i] = false;
		//}

		memset(&mPlaceStr[i][0], 0, 4); /* clear sockaddr structure */
		sprintf( mPlaceStr[i], "%d", mPlace[i]);

		//if (memcmp(mLastDriverName, di.accountName, 32) != 0) {
		//	memcpy(&mLastDriverName, &di.accountName, 32);

			

			if (numberAvailable[i]) {
				memcpy(&mDriverNumStr[i], &di.mDriverNumStr, 5);
			}
			

			if (classAvailable[i]) {
				if (di.mCategoryTextColorIndex == -1) {
					classTextData[i].colorIndex = classOriginalTextColorIndex[i];
				} else {
					classTextData[i].colorIndex = di.mCategoryTextColorIndex;
				}
				if (di.mCategoryImageColorIndex == -1) {
					classImageData[i].colorIndex = classOriginalImageColorIndex[i];
				} else {
					classImageData[i].colorIndex = di.mCategoryImageColorIndex;
				}
				
				memcpy(&mClassStickerName[i], &di.mCategoryName, 32);
			}
			if (classNameAvailable[i]) {
				memcpy(&mClassName[i], &di.mClassName, 32);
			}
			if (driverNameAvailable[i]) {
				memcpy(&mDriverName[i], &di.mDriverName, 32);
			}

			if (bestAvailable[i]) {
				if (di.mBestLapTime > 0) {
					if (viewedPilotColorIndex == i || viewedDi.mBestLapTime <= 0) {
						sprintf(bestStr[i], "%s", getTimeAsString(di.mBestLapTime, false, false, true, true).c_str());
					} else {
						if (di.mBestLapTime > viewedDi.mBestLapTime) {
							sprintf(bestStr[i], "+%s", getTimeAsString(di.mBestLapTime - viewedDi.mBestLapTime, false, false, true, true).c_str());
						} else {
							sprintf(bestStr[i], "%s", getTimeAsString(di.mBestLapTime - viewedDi.mBestLapTime, false, false, true, true).c_str());
						}
					}
				} else {
					sprintf(bestStr[i],"--:--.---");
				}
			}

			if (lastAvailable[i]) {
				if (di.mLastLapTime > 0) {
					if (viewedPilotColorIndex == i || viewedDi.mLastLapTime <= 0) {
						sprintf(lastStr[i], "%s", getTimeAsString(di.mLastLapTime, false, false, true, true).c_str());
					} else {
						if (di.mLastLapTime > viewedDi.mLastLapTime) {
							sprintf(lastStr[i], "+%s", getTimeAsString(di.mLastLapTime - viewedDi.mLastLapTime, false, false, true, true).c_str());
						} else {
							sprintf(lastStr[i], "%s", getTimeAsString(di.mLastLapTime - viewedDi.mLastLapTime, false, false, true, true).c_str());
						}
					}
				} else {
					sprintf(lastStr[i],"--:--.---");
				}
			}
		
	//}
	}
}

	std::string WidgetCompare3Driver::getTimeAsString(double seconds, bool forceAllFields, bool padHighest, bool showMillis, bool showFullMillis) {
		if ( showMillis )
            seconds = floor(0.5 + seconds * 1000 ) / 1000;
        else
            seconds = floor(0.5 + seconds );
        
		std:string str = "";

		if ( seconds < 0 )
        {
			str.append("-");
            seconds *= -1;
        }
        
        int hours = (int)( seconds / 3600 );
        
        if ( ( hours > 0 ) || forceAllFields )
        {
            if ( padHighest ) {
				if (hours < 10) {
					str.append("0");
				}
			}
			str.append(std::to_string(static_cast<long long>(hours)));
			str.append(":");
        }
        
        int minutes = ((int) ( seconds / 60 )) % 60 ;
        
        if ( ( hours > 0 ) || ( minutes > 0 ) || forceAllFields )
        {
            if ( ( hours > 0 ) || padHighest ) {
				if (minutes < 10) {
					str.append("0");
				}
			}
			str.append(std::to_string(static_cast<long long>(minutes)));
			str.append(":");
        }
        
        int restSeconds = showMillis ? ((int)floor(seconds)) % 60 : ((int)(floor(0.5 + seconds))) % 60 ;
        
        if ( ( hours > 0 ) || ( minutes > 0 ) || padHighest ) {
            if (restSeconds < 10) {
					str.append("0");
			}
		}
		str.append(std::to_string(static_cast<long long>(restSeconds)));
        
        if ( showMillis )
        {
            int millis = showFullMillis ? ( (int)floor( 0.5 + seconds * 1000 )) % 1000  : ((int) floor( 0.5 + seconds * 10 )) % 10 ;
			str.append(".");
            
            if ( showFullMillis ) {
				if (millis < 10) {
					str.append("00");
				} else if (millis < 100) {
					str.append("0");
				}
			}
			str.append(std::to_string(static_cast<long long>(millis)));
        }
		return str;
	}

void WidgetCompare3Driver::render(int profileIndex, const ScreenInfoV01 &info, const LPD3DXFONT *fontList, const D3DCOLOR *colorList, Sprite spriteList[]) { 
	if (!available) return;
		if (gReplayIsActive) return;
	if (profiles[profileIndex].hide) return;

	LPDIRECT3DDEVICE9 dev = static_cast<LPDIRECT3DDEVICE9>(info.mDevice);	
	if (bgImageData.spriteIndex > -1) {
		spriteList[bgImageData.spriteIndex].setPosition(bgImageData.left + profiles[profileIndex].widgetOffsetX, bgImageData.top + profiles[profileIndex].widgetOffsetY);
		spriteList[bgImageData.spriteIndex].setSize(bgImageData.width, bgImageData.height);
		spriteList[bgImageData.spriteIndex].render(dev, colorList[bgImageData.colorIndex], 100);
	}

	for (int i = 0; i < 3; i++) {
		if (placeAvailable[i]) {
			tmpRect.left = placeTextData[i].rect.left + profiles[profileIndex].widgetOffsetX;
			tmpRect.top = placeTextData[i].rect.top + profiles[profileIndex].widgetOffsetY;
			tmpRect.right = placeTextData[i].rect.right + profiles[profileIndex].widgetOffsetX;
			tmpRect.bottom = placeTextData[i].rect.bottom + profiles[profileIndex].widgetOffsetY;
			if (i == viewedPilotColorIndex) {
				int color = (textColorForViewedDriver == -1)?placeTextData[i].colorIndex:textColorForViewedDriver;
				int font = (textFontForViewedDriver == -1)?placeTextData[i].fontIndex:textFontForViewedDriver;
				fontList[font]->DrawTextA(NULL, mPlaceStr[i], -1, &tmpRect, placeTextData[i].alignment, colorList[color]);
			} else {
				fontList[placeTextData[i].fontIndex]->DrawTextA(NULL, mPlaceStr[i], -1, &tmpRect, placeTextData[i].alignment, colorList[placeTextData[i].colorIndex]);
			}
		} 
	
		if (numberAvailable) {
			tmpRect.left = numberTextData[i].rect.left + profiles[profileIndex].widgetOffsetX;
			tmpRect.top = numberTextData[i].rect.top + profiles[profileIndex].widgetOffsetY;
			tmpRect.right = numberTextData[i].rect.right + profiles[profileIndex].widgetOffsetX;
			tmpRect.bottom = numberTextData[i].rect.bottom + profiles[profileIndex].widgetOffsetY;

			if (i == viewedPilotColorIndex) {
				int color = (textColorForViewedDriver == -1)?numberTextData[i].colorIndex:textColorForViewedDriver;
				int font = (textFontForViewedDriver == -1)?numberTextData[i].fontIndex:textFontForViewedDriver;
				fontList[font]->DrawTextA(NULL, mDriverNumStr[i], -1, &tmpRect, numberTextData[i].alignment, colorList[color]);
			} else {
				fontList[numberTextData[i].fontIndex]->DrawTextA(NULL, mDriverNumStr[i], -1, &tmpRect, numberTextData[i].alignment, colorList[numberTextData[i].colorIndex]);
			}
		}

		// category
		if (classAvailable[i]) {
			if (classImageData[i].spriteIndex != -1) {
				spriteList[classImageData[i].spriteIndex].setPosition(classImageData[i].left + profiles[profileIndex].widgetOffsetX, classImageData[i].top + profiles[profileIndex].widgetOffsetY);
				spriteList[classImageData[i].spriteIndex].setSize(classImageData[i].width, classImageData[i].height);
				spriteList[classImageData[i].spriteIndex].render(dev, colorList[classImageData[i].colorIndex], 100);
			}
			tmpRect.left = classTextData[i].rect.left + profiles[profileIndex].widgetOffsetX;
			tmpRect.top = classTextData[i].rect.top + profiles[profileIndex].widgetOffsetY;
			tmpRect.right = classTextData[i].rect.right + profiles[profileIndex].widgetOffsetX;
			tmpRect.bottom = classTextData[i].rect.bottom + profiles[profileIndex].widgetOffsetY;

			if (i == viewedPilotColorIndex) {
				int color = (textColorForViewedDriver == -1)?classTextData[i].colorIndex:textColorForViewedDriver;
				int font = (textFontForViewedDriver == -1)?classTextData[i].fontIndex:textFontForViewedDriver;
				fontList[font]->DrawTextA(NULL, mClassStickerName[i], -1, &tmpRect, classTextData[i].alignment, colorList[color]);
			} else {
				fontList[classTextData[i].fontIndex]->DrawTextA(NULL, mClassStickerName[i], -1, &tmpRect, classTextData[i].alignment, colorList[classTextData[i].colorIndex]);
			}
		}
	
		if (classNameAvailable[i]) {
			tmpRect.left = classNameTextData[i].rect.left + profiles[profileIndex].widgetOffsetX;
			tmpRect.top = classNameTextData[i].rect.top + profiles[profileIndex].widgetOffsetY;
			tmpRect.right = classNameTextData[i].rect.right + profiles[profileIndex].widgetOffsetX;
			tmpRect.bottom = classNameTextData[i].rect.bottom + profiles[profileIndex].widgetOffsetY;

			if (i == viewedPilotColorIndex) {
				int color = (textColorForViewedDriver == -1)?classNameTextData[i].colorIndex:textColorForViewedDriver;
				int font = (textFontForViewedDriver == -1)?classNameTextData[i].fontIndex:textFontForViewedDriver;
				fontList[font]->DrawTextA(NULL, mClassName[i], -1, &tmpRect, classNameTextData[i].alignment, colorList[color]);
			} else {
				fontList[classNameTextData[i].fontIndex]->DrawTextA(NULL, mClassName[i], -1, &tmpRect, classNameTextData[i].alignment, colorList[classNameTextData[i].colorIndex]);
			}
		}

		if (driverNameAvailable[i]) {
			tmpRect.left = driverNameTextData[i].rect.left + profiles[profileIndex].widgetOffsetX;
			tmpRect.top = driverNameTextData[i].rect.top + profiles[profileIndex].widgetOffsetY;
			tmpRect.right = driverNameTextData[i].rect.right + profiles[profileIndex].widgetOffsetX;
			tmpRect.bottom = driverNameTextData[i].rect.bottom + profiles[profileIndex].widgetOffsetY;

			if (i == viewedPilotColorIndex) {
				int color = (textColorForViewedDriver == -1)?driverNameTextData[i].colorIndex:textColorForViewedDriver;
				int font = (textFontForViewedDriver == -1)?driverNameTextData[i].fontIndex:textFontForViewedDriver;
				fontList[font]->DrawTextA(NULL, mDriverName[i], -1, &tmpRect, driverNameTextData[i].alignment, colorList[color]);
			} else {
				fontList[driverNameTextData[i].fontIndex]->DrawTextA(NULL, mDriverName[i], -1, &tmpRect, driverNameTextData[i].alignment, colorList[driverNameTextData[i].colorIndex]);
			}
		}


		if (bestAvailable[i]) {
			tmpRect.left = bestTextData[i].rect.left + profiles[profileIndex].widgetOffsetX;
			tmpRect.top = bestTextData[i].rect.top + profiles[profileIndex].widgetOffsetY;
			tmpRect.right = bestTextData[i].rect.right + profiles[profileIndex].widgetOffsetX;
			tmpRect.bottom = bestTextData[i].rect.bottom + profiles[profileIndex].widgetOffsetY;

			if (i == viewedPilotColorIndex) {
				int color = (textColorForViewedDriver == -1)?bestTextData[i].colorIndex:textColorForViewedDriver;
				int font = (textFontForViewedDriver == -1)?bestTextData[i].fontIndex:textFontForViewedDriver;
				fontList[font]->DrawTextA(NULL, bestStr[i], -1, &tmpRect, bestTextData[i].alignment, colorList[color]);
			} else {
				fontList[bestTextData[i].fontIndex]->DrawTextA(NULL, bestStr[i], -1, &tmpRect, bestTextData[i].alignment, colorList[bestTextData[i].colorIndex]);
			}
		}

		if (lastAvailable[i]) {
			tmpRect.left = lastTextData[i].rect.left + profiles[profileIndex].widgetOffsetX;
			tmpRect.top = lastTextData[i].rect.top + profiles[profileIndex].widgetOffsetY;
			tmpRect.right = lastTextData[i].rect.right + profiles[profileIndex].widgetOffsetX;
			tmpRect.bottom = lastTextData[i].rect.bottom + profiles[profileIndex].widgetOffsetY;

			if (i == viewedPilotColorIndex) {
				int color = (textColorForViewedDriver == -1)?lastTextData[i].colorIndex:textColorForViewedDriver;
				int font = (textFontForViewedDriver == -1)?lastTextData[i].fontIndex:textFontForViewedDriver;
				fontList[font]->DrawTextA(NULL, lastStr[i], -1, &tmpRect, lastTextData[i].alignment, colorList[color]);
			} else {
				fontList[lastTextData[i].fontIndex]->DrawTextA(NULL, lastStr[i], -1, &tmpRect, lastTextData[i].alignment, colorList[lastTextData[i].colorIndex]);
			}
		}

	}
}
WidgetCompare3Driver::~WidgetCompare3Driver(void)
{
}
